package com.isoft.mapper;

import com.isoft.entity.DfmeArreEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface DfmeArreMapper {
    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "meta.msgId", keyColumn = "MSG_ID")
    void insertMeta(DfmeArreEntity entity);

    @Insert("MERGE INTO FLIGHT_BASE USING (SELECT #{dflt.flightId} AS FLIGHT_ID FROM DUAL) S ON (FLIGHT_BASE.FLIGHT_ID = S.FLIGHT_ID) " +
            "WHEN MATCHED THEN UPDATE SET " +
            "FLIGHT_ATTRIBUTE = #{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "FLIGHT_FILE_ID = #{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "FLIGHT_IDENTIFIER = #{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "STATUS = CASE WHEN #{dflt.status,jdbcType=VARCHAR} IN ('NORMAL', 'DELAYED', 'CANCELLED', 'BOARDING', 'DEPARTED', 'ALTERNATE', 'RETURNED', 'BOARDING_COMPLETED') THEN #{dflt.status} ELSE 'NORMAL' END, " +
            "INTERNAL_STATUS = #{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "MANUAL_STATUS = #{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "MANUAL_INTERNAL_STATUS = #{dflt.manualInternalStatus,jdbcType=VARCHAR}, " +
            "RELEASE_TIME = TO_TIMESTAMP(#{dflt.releaseTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss') " +
            "WHEN NOT MATCHED THEN INSERT (" +
            "FLIGHT_ID, FLIGHT_ATTRIBUTE, FLIGHT_FILE_ID, FLIGHT_IDENTIFIER, STATUS, " +
            "INTERNAL_STATUS, MANUAL_STATUS, MANUAL_INTERNAL_STATUS, RELEASE_TIME) " +
            "VALUES (" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "#{dflt.flightAttribute,jdbcType=VARCHAR}, " +
            "#{dflt.flightFileId,jdbcType=VARCHAR}, " +
            "#{dflt.flightIdentifier,jdbcType=VARCHAR}, " +
            "CASE WHEN #{dflt.status,jdbcType=VARCHAR} IN ('NORMAL', 'DELAYED', 'CANCELLED', 'BOARDING', 'DEPARTED', 'ALTERNATE', 'RETURNED', 'BOARDING_COMPLETED') THEN #{dflt.status} ELSE 'NORMAL' END, " +
            "#{dflt.internalStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualStatus,jdbcType=VARCHAR}, " +
            "#{dflt.manualInternalStatus,jdbcType=VARCHAR}, " +
            "TO_TIMESTAMP(#{dflt.releaseTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'))")
    int insertFlightBase(DfmeArreEntity entity);

    @Insert("INSERT INTO FLIGHT_IRREGULARITY(" +
            "FLIGHT_ID, ABNORMAL_STATUS, ABNORMAL_REASON, " +
            "INTERNAL_STATUS, INTERNAL_REASON, MANUAL_ABNORMAL_STATUS, MANUAL_ABNORMAL_REASON, " +
            "MANUAL_INTERNAL_STATUS, MANUAL_INTERNAL_REASON) " +
            "VALUES(" +
            "#{dflt.flightId,jdbcType=NUMERIC}, " +
            "'ARR', " +
            "NULL, " +
            "'ARR', " +
            "NULL, " +
            "NULL, NULL, NULL, NULL)")
    int insertIrregularity(DfmeArreEntity entity);
}